package logic

import (
	"context"
	"errors"
	"shop-switcher/models/mysql"
	"shop-switcher/pkg"

	"shop-switcher/shoprpc/internal/svc"
	"shop-switcher/shoprpc/shoprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *shoprpc.UpdateRequest) (*shoprpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	if err := user.FIndUserNAme(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	if err := user.Updated(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if user.Password != pkg.Md5(in.OldPassword) {
		return nil, errors.New("两次密码不一致")
	}
	if in.OldPassword == in.NewPassword {
		return nil, errors.New("新密码和旧密码一样 不允许修改")
	}
	if in.NewPassword != in.SckPassword {
		return nil, errors.New("确认密码和新密码不一样")
	}

	return &shoprpc.UpdateResponse{
		UserId: int64(user.ID),
	}, nil
}
